package com.bosssoft.hr.train.j2se.colletion;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.*;

/**
 * @Classname: ThreadArrayListDemo
 * @Description:该类可以使用synchronized 或者 java的锁控制程序避免冲突
 * @author: zoubinyan
 * @Date: 2023/2/22
 */

@Slf4j
public class ThreadArrayListDemo {
    /**
     * 定义productList存放测试数据
     */
    private List<String> productList = new ArrayList<>();
    /**
     * 生产者，消费者进行数据操纵
     */
    public void startTesting() throws InterruptedException {
        /**
         * 用于多线程的执行统计
         */
        CountDownLatch countDownLatch = new CountDownLatch(2);
        /**
         * 将非线程安全的productList转化为线程安全
         */
        List<String> syncProductList = Collections.synchronizedList(productList);
        // 启动生产者
        new ProducerThread(syncProductList,countDownLatch).start();
        //启动消费者
        new Thread(new ConsumerRunnable(syncProductList,countDownLatch)).start();
        //CountDownLatch.countDown()是用来线程计数器-1的，也就是新增线程运行完之后，都调用此方法将计数器变成0，最后调用await()方法，主线程就会被唤醒，继续执行其它代码
        countDownLatch.await();
    }

    /**
     * 生产者
     */
    public class ProducerThread extends Thread {
        /**
         *  计数
         */
        private CountDownLatch countDownLatch;
        /**
         *  产品列表
         */
        private List<String> productList;

        /**
         *
         * @param productList
         * @param countDownLatch
         */
        public  ProducerThread(List<String> productList, CountDownLatch countDownLatch) {
            this.countDownLatch = countDownLatch;
            this.productList = productList;
        }

        /**
         * 执行插入操作
         */
        @Override
        public void run() {
            super.run();
            int i = 1;
            try {
                while (true) {

                    productList.add("product"+i);
                    log.info("加入","product"+i);
                    Thread.sleep(100);
                    i++;
                }
            }
            catch (InterruptedException e) {
                e.printStackTrace();
                Thread.currentThread().interrupt();
            }
            finally {
                countDownLatch.countDown();
            }
        }
    }

    /**
     * 消费者
     */
    public class ConsumerRunnable implements Runnable {
        /**
         * 计数
         */
        private CountDownLatch countDownLatch;
        /**
         * 产品列表
         */
        private List<String> productList = new ArrayList<>();

        /**
         * @param productList
         * @param countDownLatch
         */
        public ConsumerRunnable(List<String>productList, CountDownLatch countDownLatch){
            this.countDownLatch = countDownLatch;
            this.productList = productList;
        }

        /**
         * 执行从尾部删除操作
         */
        @Override
        public void run() {
            try {
                while (true) {
                    if(!productList.isEmpty()) {
                        log.info("移除",productList.get(0));
                        productList.remove(0);
                        break;
                    }
                    else{
                        log.info("没有数据可以删除");
                    }
                    Thread.sleep(100);
                }
            }
            catch (InterruptedException e) {
                e.printStackTrace();
                Thread.currentThread().interrupt();
            }
            finally {
                this.countDownLatch.countDown();
            }
        }
    }
}